﻿using UnityEngine;
using System.Collections;

public class TweenTaHP : MonoBehaviour {

    UISlider _hp;
    float _nowvalue = 1;
    float _targetValue;
    float _addSpeed = 0.1f;
    float reSpeed;


    FSM _towerstate;
    StatePath _toweradd = new StatePath("add");
    StatePath _sleep = new StatePath("sleep");

    void Awake()
    {

        _hp = GetComponent<UISlider>();

        InitState();

    }
    public void InitState()
    {

        _towerstate = new FSM();
        _towerstate.AddState(_toweradd, OnOutEnter, OnOutUpdate);
        _towerstate.AddState(_sleep, OnSleepEnter);
    }

    void OnOutEnter()
    {

        UpdateValue();
    }

    void OnOutUpdate(float dt)
    {

        _nowvalue = Mathf.SmoothDamp(_nowvalue, _targetValue, ref reSpeed, _addSpeed);
        UpdateValue();
    }

    void OnSleepEnter()
    {
    }
	public void SetTowerRight(float value){

		_hp.value = value;
	}

    public void SetTowerValue(float value)
    {

        _targetValue = value;
        _towerstate.SetState(_toweradd);
    }

	public void SetTowerFull(){
		_targetValue = 1;
		_towerstate.SetState (_toweradd);
	}

	public void SetTowerZero(){
		_targetValue = 0;
		_towerstate.SetState (_toweradd);
	}

	public void SetInitTowerValue(float value){
		_nowvalue = 0;
		_targetValue = value;
		_towerstate.SetState (_toweradd);
	}

	public void SetInitBossValue(float value){
		_nowvalue = 0;
		_targetValue = value;
		_towerstate.SetState (_toweradd);
	}

	public void SetBossValue(float value){
		_targetValue = value * 2 - 1;
		_towerstate.SetState (_toweradd);
	}
		
	public void SetBossRedValue(float value){
		_targetValue = value * 2;
		_towerstate.SetState (_toweradd);
	}

	public void SetBossZeroValye(){
		_hp.value = 0;
	}
		

    void UpdateValue()
    {

        _hp.value = _nowvalue;
    }


    void Update()
    {
        _towerstate.UpdateState(Time.deltaTime);
    }
}
